#include <QtGui>

#include "src/color.h"
#include "src/sphere.h"
#include "sphereDialog.h"
#include "src/vector3d.h"
#include "objectTreeItem.h"

SphereDialog::SphereDialog(int _id, QWidget *_parent)
    : QDialog(_parent), id(_id){
    setWindowTitle(QString::fromUtf8("Criando esfera ").append(QString::number(id)));

    colorR = colorG = colorB = colorAlpha = 255;
    object = NULL;
    item = NULL;
    radius = 1;
    centerX = centerY = centerZ = 0;
    miS = 0.1;
    miK = 0.05;

    createLayout();
}

SphereDialog::SphereDialog(ObjectTreeItem *_item, QWidget *_parent)
    : QDialog(_parent), item(_item){

    object = item->getObject();
    id = object->getID();

    setWindowTitle(QString::fromUtf8("Modificando esfera ").append(QString::number(id)));

    Color color = object->getColor();
    colorR = color.r * 255;
    colorG = color.g * 255;
    colorB = color.b * 255;
    colorAlpha = color.alpha * 255;
    radius = ((Sphere *)object)->getRadius();
    Vector3D center = ((Sphere *)object)->getCenter();
    centerX = center.x;
    centerY = center.y;
    centerZ = center.z;
    miS = object->getMiS();
    miK = object->getMiK();

    createLayout();
}

SphereDialog::~SphereDialog(){
}

void SphereDialog::createLayout(){
    //--------------------------------------Raio
    QDoubleSpinBox *radiusDoubleSpinBox = new QDoubleSpinBox();
    radiusDoubleSpinBox->setRange(0.1, 50.0);
    radiusDoubleSpinBox->setSingleStep(0.1);
    radiusDoubleSpinBox->setValue(radius);
    connect(radiusDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRadius(double)));

    QHBoxLayout *radiusLayout = new QHBoxLayout;
    radiusLayout->addWidget(radiusDoubleSpinBox);
    radiusLayout->addStretch(1);

    QGroupBox *radiusGroupBox = new QGroupBox(QString::fromUtf8("Raio"));
    radiusGroupBox->setLayout(radiusLayout);


    //--------------------------------------Centro
    QLabel *centerXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *centerXDoubleSpinBox = new QDoubleSpinBox();
    centerXDoubleSpinBox->setRange(-50.0, 50.0);
    centerXDoubleSpinBox->setSingleStep(1.0);
    centerXDoubleSpinBox->setValue(centerX);
    connect(centerXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterX(double)));


    QLabel *centerYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *centerYDoubleSpinBox = new QDoubleSpinBox();
    centerYDoubleSpinBox->setRange(-50.0, 50.0);
    centerYDoubleSpinBox->setSingleStep(1.0);
    centerYDoubleSpinBox->setValue(centerY);
    connect(centerYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterY(double)));


    QLabel *centerZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *centerZDoubleSpinBox = new QDoubleSpinBox();
    centerZDoubleSpinBox->setRange(-50.0, 50.0);
    centerZDoubleSpinBox->setSingleStep(1.0);
    centerZDoubleSpinBox->setValue(centerZ);
    connect(centerZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeCenterZ(double)));


    QHBoxLayout *centerLayout = new QHBoxLayout;
    centerLayout->addWidget(centerXLabel);
    centerLayout->addWidget(centerXDoubleSpinBox);
    centerLayout->addStretch(1);
    centerLayout->addWidget(centerYLabel);
    centerLayout->addWidget(centerYDoubleSpinBox);
    centerLayout->addStretch(1);
    centerLayout->addWidget(centerZLabel);
    centerLayout->addWidget(centerZDoubleSpinBox);
    centerLayout->addStretch(1);

    QGroupBox *centerGroupBox = new QGroupBox(QString::fromUtf8("Centro"));
    centerGroupBox->setLayout(centerLayout);


    //--------------------------------------MiS
    QDoubleSpinBox *miSDoubleSpinBox = new QDoubleSpinBox();
    miSDoubleSpinBox->setRange(0.00001, 50.0);
    miSDoubleSpinBox->setSingleStep(1.0);
    miSDoubleSpinBox->setDecimals(5);
    miSDoubleSpinBox->setValue(miS);
    connect(miSDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMiS(double)));

    QHBoxLayout *miSLayout = new QHBoxLayout;
    miSLayout->addWidget(miSDoubleSpinBox);
    miSLayout->addStretch(1);

    QGroupBox *miSGroupBox = new QGroupBox(QString::fromUtf8("μ estático"));
    miSGroupBox->setLayout(miSLayout);


    //--------------------------------------MiK
    QDoubleSpinBox *miKDoubleSpinBox = new QDoubleSpinBox();
    miKDoubleSpinBox->setRange(0.00001, 50.0);
    miKDoubleSpinBox->setSingleStep(1.0);
    miKDoubleSpinBox->setDecimals(5);
    miKDoubleSpinBox->setValue(miK);
    connect(miKDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMiK(double)));

    QHBoxLayout *miKLayout = new QHBoxLayout;
    miKLayout->addWidget(miKDoubleSpinBox);
    miKLayout->addStretch(1);

    QGroupBox *miKGroupBox = new QGroupBox(QString::fromUtf8("μ dinâmico"));
    miKGroupBox->setLayout(miKLayout);


    //--------------------------------------Cor
    colorPushButton = new QPushButton(tr("Cor"));
    connect(colorPushButton, SIGNAL(clicked()), this, SLOT(changeColor()));
    setButtonColor();


    //--------------------------------------Botões
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(radiusGroupBox);
    mainLayout->addWidget(centerGroupBox);
    mainLayout->addWidget(miSGroupBox);
    mainLayout->addWidget(miKGroupBox);
    mainLayout->addWidget(colorPushButton);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void SphereDialog::setButtonColor(){
    //    QString colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n" "background-color: rgb(85, 0, 0, 255);");
    QString colorStr;
    if(colorR + colorB + colorG + colorAlpha < 600){
        colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        colorStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    colorStr.append(QString::fromUtf8("background-color: rgb("));
    colorStr.append(QString::number(colorR));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorG));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorB));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorAlpha));
    colorStr.append(QString::fromUtf8(");"));

    colorPushButton->setStyleSheet(colorStr);
}

void SphereDialog::acceptAndCreate(){
    if(!object){
        object = new Sphere(id, Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0),
                            miS, miK, radius, Vector3D(centerX, centerY, centerZ));

        emit done(object);
    }
    else
    {
        object->setMiS(miS);
        object->setMiK(miK);
        object->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));

        ((Sphere*)object)->setRadius(radius);
        ((Sphere*)object)->setCenter(Vector3D(centerX, centerY, centerZ));

    }

    emit done();

    accept();
}

void SphereDialog::changeColor(){
    QColor color = QColorDialog::getColor(QColor(colorR, colorG, colorB, colorAlpha), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorR = color.red();
        colorG = color.green();
        colorB = color.blue();
        colorAlpha = color.alpha();
    }
    setButtonColor();
}

void SphereDialog::changeRadius(double _r){
    radius = _r;
}

void SphereDialog::changeCenterX(double _x){
    centerX = _x;
}

void SphereDialog::changeCenterY(double _y){
    centerY = _y;
}

void SphereDialog::changeCenterZ(double _z){
    centerZ = _z;
}

void SphereDialog::changeMiS(double _miS){
    miS = _miS;
}

void SphereDialog::changeMiK(double _miK){
    miK = _miK;
}
